let joblistModule = (function(){
    let $tbody = $('tbody');
    async function bindHTML(){
        let str = '';
        // let result = await axios.get('/job/list');
        let result = await queryJob();// 本地缓存
        if(result.code == 0){
            result.data.forEach(data=>{
                let {
                    id,
                    name,
                    desc,
                    power
                } = data;
                //userhandle|departhandle|jobhandle|customerall
                // console.log(power)
                power = power.replace(/(userhandle)|(departhandle)|(jobhandle)|(departcustomer)|(allcustomer)|(resetpassword)|(customerall)|(customermy)/g,(_,$1,$2,$3,$4,$5,$6,$7,$8)=>{
                    if($1) return '员工操作权';
                    if($2) return '部门操作权';
                    if($3) return '职务操作权';
                    if($4) return '部门全部客户';
                    if($5) return '公司全部客户';
                    if($6) return '重置密码';
                    if($7) return '全部客户';
                    if($8) return '我的客户';
                })
                str += `<tr>
				<td class="w8">${id}</td>
				<td class="w10">${name}</td>
				<td class="w20">${desc}</td>
				<td class="w50">${power}</td>
				<td class="w12" jobId=${id}>
					<a href="javascript:;">编辑</a>
					<a href="javascript:;">删除</a>
				</td>
			</tr>`
            })
            $tbody.html(str);
        }else{
            $tbody.html('')
        }
        
    }
    // 编辑 删除 功能
    function handle(){
        // 基于事件委托
        $tbody.click(async e=>{
            let target = e.target,
                $target = $(target),
                tagName = target.tagName,
                TEXT = $target.html();
            let id = $target.parent().attr('jobId');
            if(tagName === 'A'){
                if(TEXT === '编辑'){
                    // 页面跳转
                    window.location.href = `jobadd.html?jobId=${id}`;
                    return;
                }else if(TEXT === '删除'){
                    // 删除前的确认
                    let flag = confirm(`小主，您确认删除编号为${id}的职务吗?`);
                    if(!flag) return;
                    let result = await axios.get('/job/delete',{
                        params:{
                            jobId:id
                        }
                    })
                    if(result.code == 0){
                        alert(`小主，已成功为您删除编号为${id}的职务`);
                        // 从本页面移除该职务
                        $target.parent().parent().remove();
                        return;
                    }
                    alert('小主，网络繁忙，请稍后重试');
                    return;
                }
            }
        })
    }
    return {
        init(){
            bindHTML();
            handle();
        }
    }
}())
joblistModule.init();